<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');

$subject_id = $_GET['id'];

//counting variable
$i = 0;

if (isset($_GET['p_up']))
{
	$mid_up = $_GET['mid'];
	
	$move_up=mysql_query("SELECT * FROM building_subject_mapping WHERE id = $mid_up");
	
	$priority=mysql_result($move_up,0,"priority");
	$pri_down = $priority;
	$pri_up = $priority - 1;
	
	$move_down=mysql_query("SELECT * FROM building_subject_mapping WHERE priority = '$pri_up' AND course_subject_id = '$subject_id'");

	$mid_down=mysql_result($move_down,0,"id");
	
	mysql_query("UPDATE building_subject_mapping 
		SET priority='$pri_up'
		WHERE id='$mid_up'");

	mysql_query("UPDATE building_subject_mapping 
		SET priority='$pri_down'
		WHERE id='$mid_down'");
	
}
	
if (isset($_GET['p_down']))
{
	$mid_down = $_GET['mid'];
	
	$move_down=mysql_query("SELECT * FROM building_subject_mapping WHERE id = $mid_down");
	
	$priority=mysql_result($move_down,0,"priority");
	$pri_up = $priority;
	$pri_down = $priority + 1;
	
	$move_up=mysql_query("SELECT * FROM building_subject_mapping WHERE priority = '$pri_down' AND course_subject_id = '$subject_id'");

	$mid_up=mysql_result($move_up,0,"id");
	
	mysql_query("UPDATE building_subject_mapping 
		SET priority='$pri_up'
		WHERE id='$mid_up'");

	mysql_query("UPDATE building_subject_mapping 
		SET priority='$pri_down'
		WHERE id='$mid_down'");
}

//select statement
$query="SELECT * FROM building_subject_mapping WHERE course_subject_id = $subject_id ORDER BY priority";
$result=mysql_query($query);

$find_insert=mysql_query("SELECT * FROM building_subject_mapping");
$insert_point=mysql_numrows($find_insert);

//add mapping will be done at one past last mapping currently in table
$insert_point++;

//determine dynamically number of rows read from table
$num_rows=mysql_numrows($result);

$query2="SELECT * FROM course_subjects where id = $subject_id";
$result2=mysql_query($query2);

$subject_name=mysql_result($result2,$i,"subject_title");
$abbreviation=mysql_result($result2,$i,"abbreviation");

?>
<p>
	<a href="../">&lt;&lt; Back to Subject Portal</a>
</p>
<?php
echo "<h1>$subject_name Building Mappings</h1>";
echo '<p>';
//creates link/button for add timeslot
echo "<a href='add.php?sid=$subject_id'>Add Mapping</a>";
echo '</p>';

if ($num_rows > 0)
{
	echo '<p>';

	//start table
	echo "<table class=\"postSchedule\">";
	
		//create table row with column ID's
		echo "<tr>";
			echo "<td><b>Building</b></td>";
			echo "<td width='80' style='text-align: center'><b>Priority</b></td>";
			echo "<td></td>";
			echo "<td></td>";
			echo "<td></td>";
		echo "</tr>";

	//loop to walk through and print to screen all entries in timeslots table
	$row_count=0;
	while ($i < $num_rows) 
	{
		$row_count++;
		//store each timeslotname and abbreviation in a scalar
		$building_id=mysql_result($result,$i,"building_id");
		$mapping_id=mysql_result($result,$i,"id");
		$priority=mysql_result($result,$i,"priority");

		$query2="SELECT * FROM buildings WHERE id = $building_id";
		$result2=mysql_query($query2);
		
		$building_name=mysql_result($result2,0,"building_name");
		$building_abbrev=mysql_result($result2,0,"abbreviation");
		
		//generate table row for each mapping on each iteration of the loop
		echo "<tr";
		if (($row_count%2) != 0) echo " class=\"zebra_stripe\"";
		echo ">";
			echo "<td>$building_name ($building_abbrev)</td>";	
			echo "<td style='text-align: center'>$priority</td>";		

			if (!($i == 0))
			{
				echo "<td>";		//cell containing form for priority up
					echo "<a href='index.php?id=$subject_id&amp;mid=$mapping_id&amp;p_up=true'>Priority Up</a>";
				echo "</td>";
			}
			else
			{
				echo "<td></td>";
			}
			
			if (!($i ==$num_rows-1))
			{
				echo "<td>";		//cell containing form for priority down
					echo "<a href='index.php?id=$subject_id&amp;mid=$mapping_id&amp;p_down=true'>Priority Down</a>";
				echo "</td>";
			}
			else
			{
				echo "<td></td>";
			}
			
			echo "<td>";		//cell containing form for delete mapping link/button
				echo "<a href='delete.php?id=$mapping_id'>Delete</a>";
			echo "</td>";
	
		echo "</tr>";
		
		//increment counting variable
		$i++;
	}

	//end table tag
	echo "</table>";

	echo '</p>';

}
else
{
	echo "<p><b>NO BUILDING MAPPING CURRENTLY ASSOCIATED WITH $subject_name</b></p>";
}

//close
mysql_close();




?>


<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>